package com.huaxiafinance.management.service.impl;

import com.huaxiafinance.management.dto.request.QueryFranchiseeToBeInfoDto;
import com.huaxiafinance.management.dto.response.PartnerVo;
import com.huaxiafinance.management.mapper.PackageOrderMapper;
import com.huaxiafinance.management.mapper.PartnerCommissionMapper;
import com.huaxiafinance.management.mapper.PartnerInfoMapper;
import com.huaxiafinance.management.model.PartnerInfo;
import com.huaxiafinance.management.service.PartnerInfoService;
import com.huaxiafinance.management.util.MessageResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by hank on 2017/12/4.
 */
@SuppressWarnings("SpringJavaAutowiringInspection")
@Service
public class PartnerInfoServiceImpl implements PartnerInfoService {

    private static final Logger logger = Logger.getLogger(PartnerInfoServiceImpl.class);
    @Autowired
    private PartnerInfoMapper partnerInfoMapper;
    @Autowired
    private PackageOrderMapper packageOrderMapper;
    @Autowired
    private PartnerCommissionMapper partnerCommissionMapper;


    @Override
    public MessageResult getFranchiseeInfo(QueryFranchiseeToBeInfoDto dto) {
        logger.info("【接收的请求参数为】======================" + dto);
        MessageResult result = new MessageResult();
        try {
            dto.setPage(dto.getPage() == null ? null : (dto.getPage() - 1) * dto.getRows());
            List<PartnerInfo> partnerInfos;
            Integer rows = partnerInfoMapper.getFranchiseeCount(dto);
            //加盟商数量
            Integer franchiseeNum;
            Map<String, Object> map;
            if ("grandfather_id".equals(dto.getpTypeColumn())) {
                //二级加盟商数量
                franchiseeNum = partnerInfoMapper.getSecondNum(dto);
                //二级加盟商列表
                partnerInfos = partnerInfoMapper.getSecFranchisee(dto);
                //二级加盟商购买的套餐数和套餐总金额
                map = packageOrderMapper.getSecondOrderNum(dto);
            } else {
                //准加盟商/一级加盟商数量
                franchiseeNum = partnerInfoMapper.getFranchiseeNum(dto);
                //准/一级加盟商列表
                partnerInfos = partnerInfoMapper.getFranchisee(dto);
                //准/一级级加盟商购买的套餐数和套餐总金额
                map = packageOrderMapper.getOrderNum(dto);
            }
            //套餐总金额
            map.put("totalPrice", map.get("totalPrice").toString());
            //加盟奖金
            String reward = partnerCommissionMapper.getJoinReward(dto.getUserUuid(), dto.getSourceGrade());
            map.put("partnerInfos", partnerInfos);
            map.put("franchiseeNum", franchiseeNum);
            map.put("reward", reward);
            result.setData(map);
            result.setRows(rows);
            result.setSuccessResult();
            logger.info("【接收到的响应数据】=======================" + result.getAsJSON());
            return result;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
        result.setFailureResult();
        return result;
    }

    /**
     * 通过参数获取用户的信息
     *
     * @param dto
     * @return MessageResult
     */
    @Override
    public MessageResult getAllResultByParam(QueryFranchiseeToBeInfoDto dto) {

        MessageResult result = new MessageResult();
        try {
            if (dto.getStart() == null | dto.getStart().equalsIgnoreCase("") | dto.getEnd() == null | dto.getEnd().equalsIgnoreCase("")) {
                dto.setStart(null);
                dto.setEnd(null);
            }

            dto.setPage(dto.getPage() == null ? null : (dto.getPage() - 1) * dto.getRows());

            dto.setType(dto.getGradeId());

            List<PartnerVo> partnerVoss = partnerInfoMapper.selectByParam(dto);

            Integer PFirstCount = null;

            Integer PSecnodCount = null;

            Integer Zcount = null;

            List<PartnerVo> partnerVos = new ArrayList<>();

            for (PartnerVo partnerInfo : partnerVoss) {

                String userUuid = partnerInfo.getUserUuid();

                dto.setUserUuid(userUuid);

                PFirstCount = partnerInfoMapper.selectPFirstInviterByUserUuid(dto);

                PSecnodCount = partnerInfoMapper.selectPSecondInviterByUserUuid(dto);

                Zcount = partnerInfoMapper.selectZInviterByUserUuid(dto);
                //判断已支付且不是合伙人 isPartner改为0
                if (packageOrderMapper.selectIsGet(partnerInfo.getUserUuid()) != null && partnerInfo.getIsPartner() == 0) {
                    partnerInfo.setIsPartner(0);
                } else {
                    partnerInfo.setIsPartner(1);
                }

                partnerInfo.setpFirstCount(PFirstCount);

                partnerInfo.setpSecondCount(PSecnodCount);

                partnerInfo.setzCount(Zcount);

                partnerVos.add(partnerInfo);

            }

            Integer rows = partnerInfoMapper.selectByParamCount(dto);

            Map<String, Object> map = new HashMap<>();

            map.put("partnerInfoVo", partnerVos);

            result.setData(map);
            result.setRows(rows);
            result.setSuccessResult();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            result.setFailureResult();
        }
        return result;
    }

}
